#!/usr/bin/env bash

# 使用说明　注意提前修改修改ＩＰ地址　和　hostname，手动增加hosts
# 可以使用命令 hostnamectl set-hostname XXXX
# 手动修改ip  ls /etc/sysconfig/network-scripts/下面的 ifcfg-XXXX
# 手动增加hosts解析　/etc/hosts

#MASTERHOSTNAME="full-master-node"
#MASTERIP="192.168.9.190"

# 关闭防火墙
systemctl disable firewalld.service 
systemctl stop firewalld.service

# 关闭swap
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0

# 启用ip4转发和ip6的桥接功能 ipv4这个点在很多网络的文章中是漏掉的，会导致kube-proxy不能正常工作
cat <<EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

# 关闭 selinu
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux

# 生效配置
sysctl --system

# 安装docker (cenos7 以上会安装docker 1.13.1)
yum install -y docker
systemctl enable docker
systemctl start docker

#　bash命令辅助工具
yum install bash-completion -y
echo "source <(kubectl completion bash)" >> ~/.bashrc


################以上无论是node还是master都需要准备的########################




# 从mirrorgooglecontainers源下载镜像（CoreDNS已正式上线，代替原来kube-dns组件）
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.11.1
docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.11.1
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.11.1
docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.11.1
docker pull mirrorgooglecontainers/pause-amd64:3.1
docker pull mirrorgooglecontainers/etcd-amd64:3.2.18
docker pull coredns/coredns:1.1.3
docker pull quay.io/coreos/flannel:v0.10.0-amd64

# 将镜像标记为k8s.gcr.io的名称
docker tag mirrorgooglecontainers/kube-apiserver-amd64:v1.11.1 k8s.gcr.io/kube-apiserver-amd64:v1.11.1
docker tag mirrorgooglecontainers/kube-scheduler-amd64:v1.11.1 k8s.gcr.io/kube-scheduler-amd64:v1.11.1
docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.11.1 k8s.gcr.io/kube-proxy-amd64:v1.11.1
docker tag mirrorgooglecontainers/kube-controller-manager-amd64:v1.11.1 k8s.gcr.io/kube-controller-manager-amd64:v1.11.1
docker tag mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd-amd64:3.2.18 k8s.gcr.io/etcd-amd64:3.2.18
docker tag coredns/coredns:1.1.3 k8s.gcr.io/coredns:1.1.3


# 安装、配置kubelet
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.11.1 kubeadm-1.11.1 kubectl-1.11.1
systemctl enable kubelet
systemctl start kubelet

# 初始化k8s token-ttl默认有效为24小时，改为0为永久有效。设置pod-network-cidr为flannel做准备
kubeadm init --kubernetes-version v1.11.1 --token-ttl 0 \
--pod-network-cidr 10.244.0.0/16

# 复制admin配置文件并修改所属用户和组
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 配置flannel网络插件
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml


# 确认kubernetes运行状态
kubectl get pods --all-namespaces

